package com.umeox.watch.moto.launcher.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;


/**
 * Created by Rambo 2019-08-16
 * Describe:
 */
@SuppressLint("AppCompatCustomView")
public class LoadingTextView extends TextView {

    private Handler mHandler = new Handler(Looper.getMainLooper());
    String text;

    private int index = 0;
    private Runnable loadingAnimRunnable = new Runnable() {
        @Override
        public void run() {

            switch (index) {
                case 1:
                    setText(String.format("%s .", text));
                    break;
                case 2:
                    setText(String.format("%s ..", text));
                    break;
                case 3:
                    setText(String.format("%s ...", text));
                    break;
                default:
                    break;
            }
            index++;
            if (index > 3) {
                index = 0;
            }
            mHandler.postDelayed(this, 500);
        }
    };


    public LoadingTextView(Context context) {
        this(context,null);
    }

    public LoadingTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setGravity(Gravity.START);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        text = (String) getText();
        mHandler.post(loadingAnimRunnable);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacks(loadingAnimRunnable);
    }
}
